package com.zzptc.easyshop.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.vo.Users;
import com.zzptc.easyshop.vo.VO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao implements IDAO {
    @Override//插入数据方法
    public boolean insert(VO vo){
        MysqlDB db = new MysqlDB();
        Connection conn = null;
        //执行游标
        PreparedStatement ps = null;
        int i = 0;//用于返回一个ture and false
        try {
            ps = db.getConn().prepareStatement("insert into easybuy_user(eu_user_id,eu_password) values(?,?)");
            //创建users类对象
            Users users = (Users) vo;
            //问号赋值

            ps.setString(1,users.getEu_user_name());
            ps.setString(2,users.getEu_password());
            //执行sql语句，成功即返回1
            i= ps.executeUpdate();
            //一方法executeUpdate
            //用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL（数据定义语言）语句，例如 CREATE TABLE 和 DROP TABLE。
            //INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。
            //executeUpdate 的返回值是一个整数（int），指示受影响的行数（即更新计数）。
            //二方法executeQuery
            //用于产生单个结果集（ResultSet）的语句，例如:被执行最多的SELECT 语句。
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
                db.closeAll(conn,ps,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return i>0;
    }

    @Override
    public boolean login(VO vo){
        MysqlDB db = new MysqlDB();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean i = false;
        try {
            ps = db.getConn().prepareStatement("select * from easybuy_user where eu_user_id=? and eu_password=?");
            Users users = (Users) vo;
            ps.setString(1, users.getEu_user_name());
            ps.setString(2, users.getEu_password());

            rs = ps.executeQuery();
            i = rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
                db.closeAll(conn,ps,rs);
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    @Override
    public List getAll(){
        //将List 只能接受Users类型
        List<Users> list = new ArrayList<Users>();
        IDB db = new MysqlDB();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = db.getConn();
            String sql = "select * from easybuy_user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            //循环虚拟表吗，放入集合中
            while(rs.next()){
                Users u = new Users();
                u.setEu_user_id(rs.getString(1));
                u.setEu_user_name(rs.getString(2));
                u.setEu_password(rs.getString(3));
                u.setEu_sex(rs.getString(4));
                u.setEu_birthday(rs.getString(5));
                u.setEu_identity_code(rs.getString(6));
                u.setEu_email(rs.getString(7));
                u.setEu_mobile(rs.getString(8));
                u.setEu_address(rs.getString(9));
                u.setEu_status(rs.getInt(10));
                list.add(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                db.closeAll(conn,ps,rs);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    public static void main(String[] args) {
        UserDao userdao = new UserDao();
        List<VO> listall = (ArrayList)userdao.getAll();
        String j= JSON.toJSONString(listall);
        System.out.println(j);
        List<Users> test = JSONObject.parseArray(j,Users.class);
        System.out.println(test.size());
    }
}

